<script setup>
import { storeToRefs } from 'pinia'
import { useIsCollapseStore } from '@/stores/isCollapse'
import { useTagStore } from '@/stores/tag'

// 菜单死数据
const list = ref([
  {
    path: '/',
    name: 'home',
    label: '首页',
    icon: 'house',
    url: 'Home',
  },
  {
    path: '/mall',
    name: 'mall',
    label: '商品管理',
    icon: 'video-play',
    url: 'Mall',
  },
  {
    path: '/user',
    name: 'user',
    label: '用户管理',
    icon: 'user',
    url: 'User',
  },
  {
    path: 'other',
    label: '其他',
    icon: 'location',
    children: [
      {
        path: '/page1',
        name: 'page1',
        label: '页面1',
        icon: 'setting',
        url: 'Page1',
      },
      {
        path: '/page2',
        name: 'page2',
        label: '页面2',
        icon: 'setting',
        url: 'Page2',
      },
    ],
  },
])

// 区分是否包含子菜单（根据element-plus的菜单组件要求，子菜单需要嵌套在el-sub-menu中）
// 使用计算属性可以保证list数据变化时，菜单数据也会跟着变化
const hasChildren = computed(() => list.value.filter(item => item.children))
const noChildren = computed(() => list.value.filter(item => !item.children))

// subMenu展开与收起的事件处理
const handleOpen = (key, keyPath) => {
  // console.log(key, keyPath)
}
const handleClose = (key, keyPath) => {
  // console.log(key, keyPath)
}

// 控制菜单是否折叠
const { isCollapse } = storeToRefs(useIsCollapseStore())

// 点击菜单事件
const router = useRouter()
const active = computed(() => router.currentRoute.value.path)
const { addTag } = useTagStore()
const handleClickMenu = item => {
  addTag(item)
  router.push(item.path)
}
</script>

<template>
  <el-aside :width="isCollapse ? '64px' : '200px'">
    <!-- 菜单 -->
    <el-menu
      active-text-color="#84bffb"
      background-color="#545c64"
      class="el-menu-vertical"
      :default-active="active"
      text-color="#fff"
      @open="handleOpen"
      @close="handleClose"
      :collapse="isCollapse"
      :collapse-transition="false"
    >
      <h3 v-show="!isCollapse">通用后台管理体统</h3>
      <h3 v-show="isCollapse">后台</h3>
      <el-menu-item
        v-for="(item, index) in noChildren"
        :key="index"
        :index="item.path"
        @click="handleClickMenu(item)"
      >
        <component :is="item.icon" class="icons"></component>
        <span>{{ item.label }}</span>
      </el-menu-item>
      <el-sub-menu
        v-for="item in hasChildren"
        :key="item.path"
        :index="item.path"
      >
        <template #title>
          <component :is="item.icon" class="icons"></component>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
            v-for="subItem in item.children"
            :key="subItem.path"
            @click="handleClickMenu(subItem)"
          >
            <component :is="subItem.icon" class="icons"></component>
            {{ subItem.label }}
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<style scoped lang="less">
.icons {
  width: 18px;
  height: 18px;
  margin-right: 5px;
}
.el-aside {
  background-color: #555c63;
  .el-menu {
    border-right: none;
  }
}
.el-menu-vertical {
  h3 {
    color: #fff;
    text-align: center;
    line-height: 60px;
    font-size: 16px;
  }
}
</style>
